const util = require("../../utils/util");
const {
    $Toast
} = require('../../iView/base/index');
var app = getApp();
Page({
    data: {
        userInfo: null,
        isLogin: false,
        linkList: [{
            title: "关于我们",
            link: "./about"
        }],
        count: 0
    },
    onLoad: function () {
        //检查token是否存在
        var userInfo = util.getStorageSync("userInfo");
        if (userInfo != null) {
            this.setData({
                userInfo: app.globalData.userInfo,
                isLogin: true
            });
        }
        app.http('/ConfigFunction/MyConfigInfo', '', "GET")
            .then(res => {
                if (res.resultCode == 0) {
                    this.setData({
                        linkList: res.data
                    });
                }
            });
    },
    onShow: function () {
        var self = this;
        if (!this.data.isLogin) {
            return;
        }
        //页面再次展示执行
        app.http('/CostNote/GetReadyDoVerifyCount', '', 'GET')
            .then(res => {
                if (res.resultCode == 0) {
                    this.setData({
                        count: res.data
                    });
                }
            });
    },
    /**
     * 跳转到登录页
     */
    handleToLogin: function () {
        wx.clearStorageSync();
        wx.navigateTo({
            url: '/pages/login/index'
        });
    },
    //我的待办
    handleToReadyDoList: function () {
        wx.navigateTo({
            url: "/pages/applyfor/index",
        });
    },
    //日历查看申请记录
    handleToDailyRecordList: function () {
        wx.navigateTo({
            url: "/pages/calendar/calendar",
        });
    },
    //去申请
    handleToAddDailyHistory: function () {
        console.log("handleToAddDailyHistory");
        wx.navigateTo({
            url: "/pages/registration/index",
        });
    },

    //人员分区管理
    handleToZoneManager: function () {
        wx.navigateTo({
            url: "/pages/zone-manager/index",
        });
    },

    closeMask: function () {
        this.setData({
            showModalStatus: false,
            showAvaModal: false
        });
    },

    /**
     * 刷新头像昵称
     * @param {any} e 
     */
    bindGetUserProfile(e) {
        var that = this;
        const {
            SDKVersion
        } = wx.getSystemInfoSync();
        //判断是否支持getUserProfile()获取头像昵称
        const compareRes = this.compareVersion(SDKVersion, "2.21.2");
        //不支持
        if (compareRes != -1) {
            console.log("不支持getUserProfile()获取头像");
            //弹出获取用户头像昵称弹窗组件
            that.setData({
                showModalStatus: true,
                showAvaModal: true
            });
        } else {
            //支持
            wx.getUserProfile({
                desc: '刷新你的昵称、头像、地区及性别',
                success: function (res_user) {
                    console.log('获取成功: ', res_user);
                    if (res_user.userInfo) {
                        //用户按了允许授权按钮
                        var tempUserInfo = that.data.userInfo;
                        tempUserInfo.avatarUrl = res_user.userInfo.avatarUrl;
                        tempUserInfo.nickName = res_user.userInfo.nickName;
                        that.setData({
                            userInfo: tempUserInfo
                        });
                        app.globalData.userInfo.avatarUrl = tempUserInfo.avatarUrl;
                        app.globalData.userInfo.nickName = tempUserInfo.nickName;
                        that.updateUserHeadImageNick(res_user.userInfo);
                    }
                },
                fail: function (err) {
                    console.log("获取失败: ", err);
                    $Toast({
                        content: '头像，昵称获取失败',
                        type: 'error'
                    });
                }
            });
        }
    },

    getAvaNickData(res) {
        const {
            avatarUrl,
            nickName,
            changeAvatarUrl
        } = res.detail
        console.log(avatarUrl, nickName, changeAvatarUrl);
        if (!avatarUrl) {
            $Toast({
                content: '头像不能为空',
                type: 'error'
            });
            return;
        }
        if (!nickName) {
            $Toast({
                content: '昵称不能为空',
                type: 'error'
            });
            return;
        }
        var that = this;
        if (changeAvatarUrl) {
            wx.uploadFile({
                url: app.globalData.uploadFileUrl + "0",
                filePath: avatarUrl,
                name: 'upfile',
                success: (res) => {
                    const rsp_data = util.strToJson(res.data);
                    //放入url
                    console.log({
                        index: rsp_data.Others,
                        url: rsp_data.Url,
                        fullUrl: rsp_data.FullUrl
                    });
                    const userInfo = {
                        avatarUrl: rsp_data.FullUrl,
                        nickName: nickName,
                        gender: 1
                    };
                    that.updateUserHeadImageNick(userInfo);
                },
                fail: (res) => {
                    that.setData({
                        isSaving: false
                    });
                    wx.hideLoading();
                    wx.showToast({
                        title: '文件上传失败',
                        icon: 'none',
                        duration: 1500
                    });
                }
            });
        } else {
            const userInfo = {
                avatarUrl: app.globalData.userInfo.avatarUrl,
                nickName: nickName,
                gender: 1
            };
            that.updateUserHeadImageNick(userInfo);
        }
    },

    /**
     * 更新用户头像，昵称
     */
    updateUserHeadImageNick(userInfo) {
        const that = this;
        wx.request({
            url: encodeURI(app.globalData.api + '/Account/UpdateUserHeadImageNick'),
            data: {
                token: app.globalData.userInfo.token,
                avatarUrl: userInfo.avatarUrl,
                nickName: userInfo.nickName,
                gender: userInfo.gender
            },
            method: 'GET',
            success: function (res) {
                console.log(res);
                var tempUserInfo = that.data.userInfo;
                tempUserInfo.avatarUrl = userInfo.avatarUrl;
                tempUserInfo.nickName = userInfo.nickName;
                that.setData({
                    userInfo: tempUserInfo
                });

                app.globalData.userInfo.avatarUrl = tempUserInfo.avatarUrl;
                app.globalData.userInfo.nickName = tempUserInfo.nickName;
                util.setStorageSync("userInfo", app.globalData.userInfo, 2 * 60 * 60);
                that.closeMask();
                $Toast({
                    content: '刷新成功',
                    type: 'success'
                });
            },
            fail: function (res) {
                console.log(res);
            },
            complete: function () {
                console.log('发起请求结束');
            }
        });
    },

    compareVersion(v1, v2) {
        v1 = v1.split('.');
        v2 = v2.split('.');
        const len = Math.max(v1.length, v2.length);

        while (v1.length < len) {
            v1.push('0');
        }
        while (v2.length < len) {
            v2.push('0');
        }
        for (let i = 0; i < len; i++) {
            const num1 = parseInt(v1[i]);
            const num2 = parseInt(v2[i]);
            if (num1 > num2) {
                return 1;
            } else if (num1 < num2) {
                return -1;
            }
        }
    },
    onShareAppMessage: function () {
        return {
            title: '记录你的一点一滴~',
            desc: '记录你的一点一滴~',
            path: 'pages/index/index',
            imageUrl: app.globalData.shareImgUrl
        };
    }
});